package com.ruoyi.web.controller.statistics;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.service.ICommentAfterServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 评论与售后分析控制器
 * 
 * @author ruoyi
 */
@Api(tags = "评论与售后分析")
@RestController
@RequestMapping("/statistics/comment_after_service")
public class CommentAfterServiceController extends BaseController {

    @Autowired
    private ICommentAfterServiceService commentAfterServiceService;

    /**
     * 获取评论统计数据
     */
    @ApiOperation("获取评论统计数据")
    @GetMapping("/comment/statistics")
    public AjaxResult getCommentStatistics(
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime,
            @ApiParam("商品ID") @RequestParam(required = false) Long goodId,
            @ApiParam("评分范围") @RequestParam(required = false) String ratingRange) {
        try {
            Map<String, Object> result = commentAfterServiceService.getCommentStatistics(startTime, endTime, goodId, ratingRange);
            return success(result);
        } catch (Exception e) {
            return error("获取评论统计数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取评论评分分布
     */
    @ApiOperation("获取评论评分分布")
    @GetMapping("/comment/rating-distribution")
    public AjaxResult getCommentRatingDistribution(
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime,
            @ApiParam("商品ID") @RequestParam(required = false) Long goodId) {
        try {
            Map<String, Object> result = commentAfterServiceService.getCommentRatingDistribution(startTime, endTime, goodId);
            return success(result);
        } catch (Exception e) {
            return error("获取评论评分分布失败：" + e.getMessage());
        }
    }

    /**
     * 获取评论关键词分析
     */
    @ApiOperation("获取评论关键词分析")
    @GetMapping("/comment/keywords")
    public AjaxResult getCommentKeywords(
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime,
            @ApiParam("商品ID") @RequestParam(required = false) Long goodId,
            @ApiParam("关键词数量") @RequestParam(defaultValue = "20") Integer topN) {
        try {
            Map<String, Object> result = commentAfterServiceService.getCommentKeywords(startTime, endTime, goodId, topN);
            return success(result);
        } catch (Exception e) {
            return error("获取评论关键词分析失败：" + e.getMessage());
        }
    }

    /**
     * 获取负面评价预警
     */
    @ApiOperation("获取负面评价预警")
    @GetMapping("/comment/negative-alert")
    public AjaxResult getNegativeCommentAlert(
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime,
            @ApiParam("商品ID") @RequestParam(required = false) Long goodId,
            @ApiParam("预警阈值") @RequestParam(defaultValue = "3.0") Double threshold) {
        try {
            Map<String, Object> result = commentAfterServiceService.getNegativeCommentAlert(startTime, endTime, goodId, threshold);
            return success(result);
        } catch (Exception e) {
            return error("获取负面评价预警失败：" + e.getMessage());
        }
    }

    /**
     * 获取售后统计数据
     */
    @ApiOperation("获取售后统计数据")
    @GetMapping("/after-service/statistics")
    public AjaxResult getAfterServiceStatistics(
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime,
            @ApiParam("处理状态") @RequestParam(required = false) String status,
            @ApiParam("服务类型") @RequestParam(required = false) String serviceType) {
        try {
            Map<String, Object> result = commentAfterServiceService.getAfterServiceStatistics(startTime, endTime, status, serviceType);
            return success(result);
        } catch (Exception e) {
            return error("获取售后统计数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取售后类型分布
     */
    @ApiOperation("获取售后类型分布")
    @GetMapping("/after-service/category-distribution")
    public AjaxResult getAfterServiceCategory(
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime,
            @ApiParam("处理状态") @RequestParam(required = false) String status) {
        try {
            Map<String, Object> result = commentAfterServiceService.getAfterServiceCategory(startTime, endTime, status);
            return success(result);
        } catch (Exception e) {
            return error("获取售后类型分布失败：" + e.getMessage());
        }
    }

    /**
     * 获取售后处理时长分析
     */
    @ApiOperation("获取售后处理时长分析")
    @GetMapping("/after-service/duration-analysis")
    public AjaxResult getAfterServiceDuration(
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime,
            @ApiParam("服务类型") @RequestParam(required = false) String serviceType) {
        try {
            Map<String, Object> result = commentAfterServiceService.getAfterServiceDuration(startTime, endTime, serviceType);
            return success(result);
        } catch (Exception e) {
            return error("获取售后处理时长分析失败：" + e.getMessage());
        }
    }

    /**
     * 获取雷达图数据
     */
    @ApiOperation("获取雷达图数据")
    @GetMapping("/radar-chart")
    public AjaxResult getRadarChartData(
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime,
            @ApiParam("商品ID") @RequestParam(required = false) Long goodId) {
        try {
            Map<String, Object> result = commentAfterServiceService.getRadarChartData(startTime, endTime, goodId);
            return success(result);
        } catch (Exception e) {
            return error("获取雷达图数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取树状图数据
     */
    @ApiOperation("获取树状图数据")
    @GetMapping("/tree-chart")
    public AjaxResult getTreeChartData(
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime,
            @ApiParam("分类类型") @RequestParam(defaultValue = "category") String type) {
        try {
            Map<String, Object> result = commentAfterServiceService.getTreeChartData(startTime, endTime, type);
            return success(result);
        } catch (Exception e) {
            return error("获取树状图数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取词云图数据
     */
    @ApiOperation("获取词云图数据")
    @GetMapping("/word-cloud")
    public AjaxResult getWordCloudData(
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime,
            @ApiParam("商品ID") @RequestParam(required = false) Long goodId,
            @ApiParam("最大词数") @RequestParam(defaultValue = "50") Integer maxWords) {
        try {
            Map<String, Object> result = commentAfterServiceService.getWordCloudData(startTime, endTime, goodId, maxWords);
            return success(result);
        } catch (Exception e) {
            return error("获取词云图数据失败：" + e.getMessage());
        }
    }
}